/*
 * Copyright 2013 Agustina Martinez-Garcia

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package uk.ljmu.qudexapps.swfspreadsheet.validators;

import java.util.List;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

//import uk.ljmu.qudexapps.swfspreadsheet.domain.DatastreamDescription;
import uk.ljmu.qudexapps.swfspreadsheet.domain.MdSchemaInstance;
import uk.ljmu.qudexapps.swfspreadsheet.domain.VocabularyElement;

public class MetadataValidator implements Validator {

	/* (non-Javadoc)
	 * @see org.springframework.validation.Validator#supports(java.lang.Class)
	 */
	public boolean supports(Class<?> arg0) {
		return arg0.equals(MdSchemaInstance.class);
	}

	/* (non-Javadoc)
	 * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
	 */
	public void validate(Object command, Errors errors) {
		MdSchemaInstance mdSchemaInstance = (MdSchemaInstance) command;
		
		if (!findSelectedElements(mdSchemaInstance.getSelectedSchemaElements())) {
			errors.reject("No elements selected for the given schema");
		}
	}

	private boolean findSelectedElements(List<VocabularyElement> vocelemArray) {
		if (vocelemArray.size() != 0) {
			for(VocabularyElement elem : vocelemArray) {
				if (elem.isSelected())
					return true;
			}
		}
		return false;
	}
}
